// api
import { saveOrUpdate, getDetail, methodGetApi } from '@/api/publicApi'
// 混入
import { publicMixins } from '@/mixins/publicMixins/publicMixins'
// formMixins
export const formMixins = {
  mixins: [publicMixins],
  data() {
    return {
      formLoading: false
    }
  },
  mounted() {
    window.addEventListener('resize', this.resizeViewConfig())
  },
  methods: {
    /**
     * @description: :新增/修改
     * @Author: baoxuhui
     * @param {*} url//请求路径
     * @param {*} data//入参
     * @param {*} type//保存/修改
     * @param {*} resetBool //回调后是否调取resetFormMixins()    布尔类型 默认:false
     * @return {*}
     */
    saveOrUpdateMixins(url, data, type, resetBool) {
      console.log(resetBool)
      return new Promise((resolve) => {
        this.formLoading = true
        saveOrUpdate(url, data).then(res => {
          if (this.sentencedEmpty(this.form.deleteAnnexIds) && this.form.deleteAnnexIds.length > 0) {
            const url = '/sys/annex/removeByIds'
            const dataIds = { ids: this.form.deleteAnnexIds.toString() }
            methodGetApi(url, dataIds).then(Response => {
              this.msgAlt(type + '成功', 'success')
              if (!resetBool) {
                this.resetFormMixins()
              }

              this.formLoading = false
              resolve(res)
            })
          } else {
            this.msgAlt(type + '成功', 'success')
            if (!resetBool) {
              this.resetFormMixins()
            }
            this.formLoading = false
            resolve(res)
          }
        }).catch((error) => {
          resolve(error)
          this.formLoading = false
        })
      })
    },

    /**
     * @description: 表单详情
     * @Author: baoxuhui
     * @param {*} url//请求路径
     * @param {*} data//入参
     * @return {*}
     */
    getDetailMixins(url, data) {
      this.formLoading = true
      getDetail(url, data).then(res => {
        this.formDetail(res)// 返回每个表单的数据
      }).catch(() => {
        this.formLoading = false
      })
    },

    /**
     * @description: 表单页点击取事件
     * @Author: baoxuhui
     * @return {*}
     */
    resetFormMixins() {
      const queryJumpDataNew = this.$route_query ? this.$route_query : {}
      const newBackQuery = {}// 返回时的参数
      if (this.sentencedEmpty(this.$route_query)) {
        if (this.$route_query.backQuery) {
          // 参数形式传入
          const newBackQuerys = JSON.parse(this.$route_query.backQuery)
          // console.log(newBackQuery)
          Object.assign(newBackQuery, newBackQuerys)
        }
      }
      if (this.$refs['form']) {
        this.$refs['form'].resetFields()
      }
      console.log('queryJumpDataNew------------', queryJumpDataNew)
      this.$store.dispatch('tagsView/delView', this.$route)// 销毁当前界面
      // entranceType：0：首页，1：工作台，2：上一页
      if (queryJumpDataNew.entranceType === '0') {
        this.$store.dispatch('tagsView/delView', this.$route) // 销毁当前界面
        this.$router.push({ path: '/index' })
      } else if (queryJumpDataNew.entranceType === '1') {
        this.$store.dispatch('tagsView/delView', this.$route) // 销毁当前界面
        this.$router.push({ path: '/BacklogList', query: queryJumpDataNew })
      } else if (queryJumpDataNew.entranceType === '2') {
        this.$store.dispatch('tagsView/delView', this.$route) // 销毁当前界面
        this.$router.go(-1) // 返回上一页
      } else {
        if (this.$route.meta.parentPath) {
          this.$store.dispatch('tagsView/delView', this.$route) // 销毁当前界面
          this.$router.push({ path: this.$route.meta.parentPath, query: newBackQuery })
        }
      }
    }
  }
}
